const TokenKey = 'Admin-Token'
export const  serverurl ='http://localhost:8080' // 'https://shop.kystu.cn/prod-api'
export function getToken() {
	if (uni.getStorageSync(TokenKey))
		return uni.getStorageSync(TokenKey);
	else
		return 'notoken';
}
export function setToken:void(token) {
	return uni.setStorageSync(TokenKey, token);

}
export function removeToken() {
	return uni.removeStorageSync(TokenKey)
}
export function parseStrEmpty(str) {
	if (!str || str == "undefined" || str == "null") {
		return "";
	}
	return str;
}
// 封装请求，如果服务器没有启动，会有跨域错误提醒
export function request(url, method = "GET", data = {}, Content_Type = "application/json;charset=UTF-8",istoken=true) {
	url = serverurl + url;
	return new Promise((resolve, reject) => {
		if(istoken == false)
		{
			uni.request({
				url,
				data,
				method,
				header: {
					'Accept': '*/*',
					// 'Access-Control-Allow-Origin': '*',
					'Content-Type': Content_Type
				},
				success: (res) => {resolve(res)},
				fail: (err) => {reject(err)}
			})			
		}
		else
		{
			uni.request({
				url,
				data,
				method,
				header: {
					'Accept': '*/*',
					// 'Access-Control-Allow-Origin': '*',
					'Content-Type': Content_Type,
					"Authorization": 'Bearer ' + getToken() //让每个请求携带自定义token
				},
				success: (res) => {resolve(res)},
				fail: (err) => {reject(err)}
			})			
		}
	})
}